package com.six.order.controller;

import com.rabbitmq.client.Channel;
import com.six.core.entity.Goods;
import com.six.core.entity.Order;
import com.six.core.util.JwtUtil;
import com.six.core.util.WebUtil;
import com.six.core.vo.Result;
import com.six.order.service.OrderService;
import com.six.order.vo.DeliverGoodsVo;
import com.six.order.vo.OrderShopVo;
import com.six.order.vo.OrderUserVo;
import com.six.order.vo.dto.GoodsCartVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jdk.nashorn.internal.runtime.linker.LinkerCallSite;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author: WangPeng
 * @createTime: 2023/03/04 10:55
 * @description:
 * @version: v1.0
 */
@Api(value = "/order/order", tags = {":"})
@RestController
@RequestMapping("/order/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @ApiOperation(value = "", notes = "")
    private Integer getUserid(){
        HttpServletRequest request = WebUtil.getRequest();
        String token = request.getHeader("token");
        Map<String, Object> map = JwtUtil.decodeJWT(token);
        System.out.println("!!!!!!!!!!!!!!!!!!"+map.get("tbUserid"));
        return (Integer) map.get("tbUserid");
    }
    @ApiOperation(value = "", notes = "")
    private Integer getShopId(){
        HttpServletRequest request = WebUtil.getRequest();
        String token = request.getHeader("token");
        Map<String, Object> map = JwtUtil.decodeJWT(token);
        return (Integer) map.get("shopId");
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "List<Order>", name = "orderList", value = "", required = true)
    })
    @ApiOperation(value = "", notes = "", httpMethod = "POST")
    @PostMapping("/createOrder")
    public Result createOrder(@RequestBody List<Order> orderList){
        for (Order order : orderList) {
            order.setUserid(getUserid());
        }
        return orderService.createOrder(orderList);
    }

    @PostMapping("/createCarOrder/{receiveid}")
    public Result createCarOrder(@RequestBody List<GoodsCartVo> goodsCartVoList,@PathVariable String receiveid){

        List<Order> orders = new ArrayList<>();
        for (GoodsCartVo vo : goodsCartVoList) {
            Order order = new Order();
            order.setUserid(getUserid());
            order.setGoodsid(vo.getGoodsid());
            order.setGoodsnum(vo.getGoodsnum());
            order.setPay(vo.getGoodsnum()*new Double(vo.getOutprice()));
            order.setReceiveid(receiveid);
            order.setShopid(new Integer(vo.getShopid()));
            orders.add(order);
        }
       return orderService.createOrder(orders);
    }

    //查询成交量
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "int", name = "id", value = "", required = true)
    })
    @ApiOperation(value = "查询成交量", notes = "查询成交量", httpMethod = "POST")
    @PostMapping("/selectPaystateByGoodsid/{id}")
    public Result selectPaystateByGoodsid(@PathVariable Integer id){
        return orderService.selectPaystateByGoodsid(id);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "OrderUserVo", name = "orderUserVo", value = "", required = true)
    })
    @ApiOperation(value = "", notes = "", httpMethod = "POST")
    @PostMapping("/userSelect")
    public Result userSelect(int currentPage, int pageSize,@RequestBody OrderUserVo orderUserVo){
        orderUserVo.setUserid(getUserid());
        System.out.println("=============="+getUserid());
       return orderService.userSelect(currentPage,pageSize,orderUserVo);
    }

    @ApiOperation(value = "", notes = "", httpMethod = "POST")
    @PostMapping("/shopSelect")
    public Result shopSelect(int currentPage, int pageSize, @RequestBody OrderShopVo orderShopVo){
        orderShopVo.setShopId(getShopId());
        return orderService.shopSelect(currentPage,pageSize,orderShopVo);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "DeliverGoodsVo", name = "deliverGoodsVo", value = "", required = true)
    })
    @ApiOperation(value = "", notes = "", httpMethod = "POST")
    @PostMapping("/deliverGoods")
    public Result deliverGoods(@RequestBody DeliverGoodsVo deliverGoodsVo){
        deliverGoodsVo.setShopid(getShopId());
        return orderService.deliverGoods(deliverGoodsVo);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "DeliverGoodsVo", name = "deliverGoodsVo", value = "", required = true)
    })
    @ApiOperation(value = "", notes = "", httpMethod = "POST")
    @PostMapping("/receiving")
    public Result receiving(@RequestBody DeliverGoodsVo deliverGoodsVo){
        return orderService.receiving(deliverGoodsVo);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "orderid", value = "", required = true)
    })
    @ApiOperation(value = "", notes = "", httpMethod = "POST")
    @PostMapping("/deleteOrder/{orderid}")
    public Result deleteOrder(@PathVariable String orderid){
        return orderService.deleteOrder(orderid);
    }


    @PostMapping("/quXiaoOrder/{orderId}")
    public Result quXiaoOrder(@PathVariable String orderId){
        return orderService.quXiaoOrder(orderId);
    }

    /** 监听死信队列 获取订单id，根据订单id查询该订单的支付状态，超时未支付取消订单*/
    @RabbitListener(queues = {"dead_queue"})
    public void quXiaoOrder2(Message msg){
        byte[] body = msg.getBody();
        String orderId = new String(body);
        System.out.println("---------------------"+orderId);
        orderService.selectOrderStatus(orderId);
    }

}
